"""
处理尺寸相关数据
"""
import re

from excutor.excutor import Excutor

class Size(Excutor):

    def __init__(self, data):
        super().__init__(data)

        self.size = r'厚度|宽度|尺寸|间距|长度|径|行距|截面积'

    @staticmethod
    def Existence(data,size = r'厚度|宽度|尺寸|间距|长度|径|行距|截面积'):
        return data[data['new_key'].str.contains(size)]['new_key'].count()
    
    #修改数据表
    def processingData(self):
        indexSize = self.data[self.data['new_key'].str.contains(self.size)].index
        for index in indexSize:
            newValueRef = self._get_new_value(index)
            self.data.loc[index,'new_value_ref'] = newValueRef
        return self.data
    
    #分析并处理old_value,返回new_value_ref
    def _get_new_value(self,index):
        oneRowData = self.data.loc[index]
        old_value = oneRowData["old_value"]
        if '\"' in old_value or re.search("in$|inch$",old_value):
            return self._inch_unit(old_value)
        units = re.findall('[muµnd]?m',old_value)
        numbers = re.findall(r'\.?[0-9]+\.?[0-9]*',old_value)
        if numbers:
            if '...' not in old_value:
                numbers = ['0'+num if num[0]=='.' else num for num in numbers]
            else:
                numbers = [ num[1:] if num[0]=='.' else num for num in numbers]   
            numbers.sort(key=float)
        ## 线径为AWG时的情况
        if 'AWG' in old_value.upper():
            new_value = self._have_AWG(oneRowData)
            if new_value:
                return new_value
        if not units:
            units = re.findall('[muµnd]?m',oneRowData["new_key"])
        if numbers:
            if units:
                if re.search('\[|~|-',old_value):
                    return numbers[0] + ' to ' + numbers[-1] + units[0]
                return ','.join([num+units[0] for num in numbers])
            if re.search('\[|~|-',old_value):
                return numbers[0] + ' to ' + numbers[-1] + 'mm'
            return ','.join([num+'mm' for num in numbers])  
        return old_value
    
    ## 处理带有双引号的长度,双引号是inch
    def _inch_unit(self,old_value):
        numAndunits = re.findall(r'\.?[0-9]+\.?[0-9]*mm',old_value.replace(' ',''))
        numAndunits = ['0'+num if num[0]=='.' else num for num in numAndunits]
        numAndunits = list(set(numAndunits))
        if numAndunits:
            if len(numAndunits) == 1:
                return numAndunits[0]
            if 'x' in old_value:
                return numAndunits[0] + 'x' + numAndunits[-1]
            if len(numAndunits) >= 1:
                sortedList = sorted(list(numAndunits),key=lambda x:float(x[:-2]))
                return ','.join(sortedList)  
        return old_value
    
    ## value中存在AWG的情况
    def _have_AWG(self,oneRowData):
        areaPattern = re.compile('线径|截面积')
        key = oneRowData['new_key']
        old_value = oneRowData['old_value']
        if not areaPattern.findall(key):
            return
        
        numWithAWG = re.findall(
            '[0-9]*/?0?[ –-]*[0-9]+/?0? *AWG|AWG *[0-9]+/?0?[ –-]*[0-9]*/?0?',
            old_value.upper()
            )
        numWithAWG = ''.join(numWithAWG)

        nums = re.findall('[0-9]+/?0?',numWithAWG)
        if len(nums) > 1:
            nums = [nums[0],nums[-1]]

        return ' to '.join(nums) + 'AWG'

    

